package cn.yangliu.nacos.management.task;

import java.util.Arrays;
import java.util.List;

import cn.yangliu.nacos.management.service.oauth2.Oauth2AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * The type of nacos.
 *
 * @author 问道于盲
 * @date 2020 -01-01
 */
@Component
public class ServiceSyncTask {

    /**
     * current class instance's member.
     * The Authority service.
     */
    @Autowired
    private Oauth2AuthorityService authorityService;

    /**
     * current class instance's member.
     * The Discovery client.
     */
    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * current class's static member
     * The constant exlutionServices.
     */
    private static final List<String> exlutionServices = Arrays.asList("oauth2", "management");

    /**
     * 将微服务信息同步到数据库
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void sync() {
        List<String> services = discoveryClient.getServices();
        services.removeIf(exlutionServices::contains);
        authorityService.syncDb(services);
    }
}
